package com.example.springbootplm.controller;

import com.example.springbootplm.pojo.Dept;
import com.example.springbootplm.pojo.Result;
import com.example.springbootplm.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    @RequestMapping("/depts")

    /*查询部门信息*/
    //private static final Logger log = LoggerFactory.getLogger(DeptController.class);
    @GetMapping
    public Result list(){

        List<Dept> deptList = deptService.list();
        log.info("查询全部部门数据");
        return Result.success(deptList);
    }


    /*删除部门信息*/
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id ){
        log.info("根据id删除部门：{}",id);
        deptService.delete(id);
        return Result.success();
    }

    /*新增部门信息*/
    @PostMapping
    public Result add(@RequestBody Dept dept){
        log.info("增加部门：{}",dept);
        deptService.add(dept);
        return Result.success();
    }


    /*根据id修改部门*/
    /*第一步，根据id查找部门*/
    @GetMapping("/depts/{id}")
    public Result check(@PathVariable Integer id){
        Dept dept =deptService.getDeptById(id);
      return Result.success(dept);
    }

    //*第二部根据id修改部门*//*
    @PutMapping("/depts/update")
    public Result update(Dept dept){
        Boolean success =deptService.update(dept);
        return success ? Result.success() : Result.error("更新失败");
    }



}
